/**
 * Copyright © 2016-2022 The Thingsboard Authors
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.thingsboard.server.dao.service.validator;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.iotconfig.IotConfigDao;
import org.thingsboard.server.dao.iotconfig.IotConfigServiceImpl;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
import org.thingsboard.server.dao.tenant.TenantDao;
import org.thingsboard.server.common.data.IotConfig;

@Component
public class IotConfigDataValidator extends DataValidator<IotConfig> {

    @Autowired
    private IotConfigDao iotConfigDao;

    @Autowired
    private TenantDao tenantDao;

    @Autowired
    @Lazy
    private TbTenantProfileCache tenantProfileCache;

    @Override
    protected void validateCreate(TenantId tenantId, IotConfig iotConfig) {
        DefaultTenantProfileConfiguration profileConfiguration =
                (DefaultTenantProfileConfiguration) tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
        long maxIotConfigs = profileConfiguration.getMaxIotConfigs();

        validateNumberOfEntitiesPerTenant(tenantId, iotConfigDao, maxIotConfigs, EntityType.IOT_CONFIG);
        iotConfigDao.findIotConfigsByTenantIdAndTitle(iotConfig.getTenantId().getId(), iotConfig.getTitle()).ifPresent(
                c -> {
                    throw new DataValidationException("IotConfig with such title already exists!");
                }
        );
    }

    @Override
    protected void validateUpdate(TenantId tenantId, IotConfig iotConfig) {
        iotConfigDao.findIotConfigsByTenantIdAndTitle(iotConfig.getTenantId().getId(), iotConfig.getTitle()).ifPresent(
                c -> {
                    if (!c.getId().equals(iotConfig.getId())) {
                        throw new DataValidationException("IotConfig with such title already exists!");
                    }
                }
        );
    }

    @Override
    protected void validateDataImpl(TenantId tenantId, IotConfig iotConfig) {
        if (StringUtils.isEmpty(iotConfig.getTitle())) {
            throw new DataValidationException("IotConfig title should be specified!");
        }
        if (iotConfig.getTitle().equals(IotConfigServiceImpl.PUBLIC_IOTCONFIG_TITLE)) {
            throw new DataValidationException("'Public' title for iotConfig is system reserved!");
        }
        if (iotConfig.getTenantId() == null) {
            throw new DataValidationException("IotConfig should be assigned to tenant!");
        } else {
            Tenant tenant = tenantDao.findById(tenantId, iotConfig.getTenantId().getId());
            if (tenant == null) {
                throw new DataValidationException("IotConfig is referencing to non-existent tenant!");
            }
        }
    }

}
